home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / fileio.cq / fileio.c
Encoding:
Text File  |  1984-04-04  |  3.3 KB  |  98 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* This function was written by Jim Niederriter for the Lattice `C' Compiler*/
  4. /* on the IBM Personal Computer or compatible, running DOS 2.0.             */
  5. /*                                                                          */
  6. /****************************************************************************/
  7. /*  fileio.c  */
  8.  
  9. /*  This file is a group of several useful I/O functions */
  10.  
  11. /*  open_fil()  */
  12. /*  this function opens a disk file in untranslated mode. The arguments */
  13. /*  required are the filename pointer, and the mode of operation.  */
  14. /*  It returns the file number.  */
  15. /*  Valid modes are: 0=read, 1=write, 2=read/write  */
  16. /*  NOTE: Untranslated mode is required for binary files, but not for */
  17. /*  ASCII files. Untranslated is specified by turning on bit 15 of    */
  18. /*  the MODE value.  */
  19.  
  20. int open_fil(filename, mode)
  21. char filename[];
  22. int mode;
  23. {
  24.      int f1;                            /* declare file numberint */
  25.      mode |= 0x8000;                    /* set on bit 15 of mode  */
  26.      f1 = open(filename, mode);
  27.      return(f1);                        /* return file number  */
  28. }รจ
  29.  
  30.  
  31. /*  get_rec()  */
  32. /*  this function gets a specific record from a random file. The */
  33. /*  required arguments are the file number, file length, relative record */
  34. /*  number, and a pointer to the input data structure.  */
  35. /*  It returns 0 if successful, or 1 if unsuccessful (read past EOF) */
  36.  
  37. int get_rec(filenum, filelgth, recno, structur)
  38. int filenum, filelgth, recno;
  39. char  structur[];
  40. {
  41.      int stats;
  42.      long offset, rtrn, lseek();
  43.  
  44.      offset = (long) (recno-1) * filelgth;
  45.      rtrn = lseek(filenum, offset, 0);           
  46.      stats = read(filenum, structur, filelgth);
  47.      if (stats > 0l) {
  48.           return(0);
  49.      }    else {
  50.                return(1);
  51.           }
  52. }
  53.  
  54. /*  wrt_rec()  */
  55. /*  this function writes a specific record to a random file. The */
  56. /*  required arguments are the file number, file length, relative record */
  57. /*  number, and a pointer to the input data structure.  */
  58. /*  It returns 0 if successful, or 1 if unsuccessful */
  59.  
  60. int wrt_rec(filenum, filelgth, recno, structur)
  61. int filenum, filelgth, recno;
  62. char structur[];
  63. {
  64.      int stats, c;
  65.      long offset, rtrn, lseek();
  66.  
  67.      offset = (long) (recno-1) * filelgth;
  68.      rtrn = lseek(filenum, offset, 0);
  69.      stats = write(filenum, structur, filelgth);
  70.      if (stats > 0l) {
  71.           return(0);
  72.      } else {
  73.           return(1);
  74.      }
  75. }
  76.  
  77. /*  wrt_add()  */
  78. /*  this function appends a record to the end of a sequential file. The  */
  79. /*  required arguments are the file number, file length, and a pointer  */
  80. /*  to the input data structure.  */
  81. /*  It returns 0 if successful, or 1 if unsuccessful */
  82.  
  83. int wrt_add(filenum, filelgth, structur)
  84. int filenum, filelgth;
  85. char structur[];
  86. {
  87.      int stats, c;
  88.      long rtrn, lseek();
  89.  
  90.      rtrn = lseek(filenum, 0L, 2);     
  91.      stats = write(filenum, structur, filelgth);
  92.      if (stats > 0l) {
  93.           return(0);
  94.      } else {
  95.           return(1);
  96.      }
  97. }
  98.